using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;

namespace HIS
{
    public class TemplateForm : Form
    {
        private ComboBox cmbTemplateType;
        private TextBox txtTemplateContent;
        private Button btnSave;
        private Button btnApprove;
        private DataGridView dgvTemplateList;

        public TemplateForm()
        {
            InitializeComponents();
        }

        private void InitializeComponents()
        {
            // 窗体设置
            this.Text = "模板维护";
            this.Size = new Size(800, 600);
            this.StartPosition = FormStartPosition.CenterScreen;

            // 模板类型选择区域
            GroupBox gbTemplateType = new GroupBox
            {
                Text = "模板类型",
                Location = new Point(10, 10),
                Size = new Size(780, 60)
            };

            Label lblTemplateType = new Label
            {
                Text = "模板类型：",
                Location = new Point(10, 25),
                AutoSize = true
            };

            cmbTemplateType = new ComboBox
            {
                Location = new Point(90, 22),
                Size = new Size(200, 25),
                DropDownStyle = ComboBoxStyle.DropDownList
            };
            cmbTemplateType.Items.AddRange(new string[] { "全院模板", "科室模板", "个人模板" });

            gbTemplateType.Controls.AddRange(new Control[] {
                lblTemplateType,
                cmbTemplateType
            });

            // 模板编辑区域
            GroupBox gbTemplateContent = new GroupBox
            {
                Text = "模板内容",
                Location = new Point(10, 80),
                Size = new Size(780, 200)
            };

            txtTemplateContent = new TextBox
            {
                Location = new Point(10, 25),
                Size = new Size(760, 165),
                Multiline = true
            };

            gbTemplateContent.Controls.Add(txtTemplateContent);

            // 操作按钮区域
            GroupBox gbOperations = new GroupBox
            {
                Text = "操作",
                Location = new Point(10, 290),
                Size = new Size(780, 60)
            };

            btnSave = new Button
            {
                Text = "保存模板",
                Location = new Point(10, 25),
                Size = new Size(100, 30)
            };

            btnApprove = new Button
            {
                Text = "审核模板",
                Location = new Point(120, 25),
                Size = new Size(100, 30)
            };

            gbOperations.Controls.AddRange(new Control[] {
                btnSave,
                btnApprove
            });

            // 模板列表显示区域
            GroupBox gbTemplateList = new GroupBox
            {
                Text = "模板列表",
                Location = new Point(10, 360),
                Size = new Size(780, 230)
            };

            dgvTemplateList = new DataGridView
            {
                Location = new Point(10, 25),
                Size = new Size(760, 195),
                AllowUserToAddRows = false,
                AllowUserToDeleteRows = false,
                ReadOnly = true,
                SelectionMode = DataGridViewSelectionMode.FullRowSelect
            };

            // 添加列
            dgvTemplateList.Columns.AddRange(new DataGridViewColumn[] {
                new DataGridViewTextBoxColumn { Name = "模板名称", HeaderText = "模板名称", Width = 200 },
                new DataGridViewTextBoxColumn { Name = "模板类型", HeaderText = "模板类型", Width = 100 },
                new DataGridViewTextBoxColumn { Name = "创建时间", HeaderText = "创建时间", Width = 150 },
                new DataGridViewTextBoxColumn { Name = "创建人", HeaderText = "创建人", Width = 100 },
                new DataGridViewTextBoxColumn { Name = "审核状态", HeaderText = "审核状态", Width = 100 }
            });

            gbTemplateList.Controls.Add(dgvTemplateList);

            // 添加控件到窗体
            this.Controls.AddRange(new Control[] {
                gbTemplateType,
                gbTemplateContent,
                gbOperations,
                gbTemplateList
            });

            // 添加模板类型选择事件
            cmbTemplateType.SelectedIndexChanged += (s, e) =>
            {
                LoadTemplates();
            };

            // 添加保存按钮事件
            btnSave.Click += (s, e) =>
            {
                SaveTemplate();
            };

            // 添加审核按钮事件
            btnApprove.Click += (s, e) =>
            {
                ApproveTemplate();
            };

            // 添加模板列表选择事件
            dgvTemplateList.SelectionChanged += (s, e) =>
            {
                if (dgvTemplateList.SelectedRows.Count > 0)
                {
                    LoadTemplateContent();
                }
            };
        }

        private void LoadTemplates()
        {
            dgvTemplateList.Rows.Clear();
            string templateType = cmbTemplateType.SelectedItem?.ToString();
            if (string.IsNullOrEmpty(templateType))
                return;

            // TODO: 从数据库加载模板列表
            // 示例代码：
            // var templates = Database.GetTemplates(templateType);
            // foreach (var template in templates)
            // {
            //     dgvTemplateList.Rows.Add(
            //         template.Name,
            //         template.Type,
            //         template.CreateTime,
            //         template.Creator,
            //         template.ApprovalStatus
            //     );
            // }
        }

        private void SaveTemplate()
        {
            string templateType = cmbTemplateType.SelectedItem?.ToString();
            string content = txtTemplateContent.Text.Trim();

            if (string.IsNullOrEmpty(templateType))
            {
                MessageBox.Show("请选择模板类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(content))
            {
                MessageBox.Show("请输入模板内容", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // TODO: 保存模板到数据库
            // 示例代码：
            // var template = new Template
            // {
            //     Type = templateType,
            //     Content = content,
            //     Creator = CurrentUser.Name,
            //     CreateTime = DateTime.Now,
            //     ApprovalStatus = "待审核"
            // };
            // Database.SaveTemplate(template);
            // LoadTemplates();
            // MessageBox.Show("模板保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void ApproveTemplate()
        {
            if (dgvTemplateList.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要审核的模板", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var row = dgvTemplateList.SelectedRows[0];
            string templateName = row.Cells["模板名称"].Value.ToString();
            string currentStatus = row.Cells["审核状态"].Value.ToString();

            if (currentStatus != "待审核")
            {
                MessageBox.Show("只能审核待审核状态的模板", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // TODO: 审核模板
            // 示例代码：
            // var result = MessageBox.Show(
            //     $"确定要审核通过模板"{templateName}"吗？",
            //     "确认",
            //     MessageBoxButtons.YesNo,
            //     MessageBoxIcon.Question
            // );
            // if (result == DialogResult.Yes)
            // {
            //     Database.ApproveTemplate(templateName, CurrentUser.Name);
            //     LoadTemplates();
            //     MessageBox.Show("模板审核通过", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            // }
        }

        private void LoadTemplateContent()
        {
            if (dgvTemplateList.SelectedRows.Count == 0)
                return;

            var row = dgvTemplateList.SelectedRows[0];
            string templateName = row.Cells["模板名称"].Value.ToString();

            // TODO: 从数据库加载模板内容
            // 示例代码：
            // var template = Database.GetTemplateContent(templateName);
            // txtTemplateContent.Text = template.Content;
        }
    }
} 